﻿package mortal.game.view.common
{
    import com.gengine.global.*;
    import com.mui.controls.*;
    import flash.events.*;

    public class IconProgressBar extends BaseProgressBar
    {
        protected var _iconBtn:GLoadedButton;
        protected var _isCanDrag:Boolean = false;
        protected var _baseX:Number = 0;
        protected var _baseY:Number = 0;
        protected var _baseHeight:Number = 0;
        private var _mouseStartX:Number;
        private var _iconBaseX:Number;

        public function IconProgressBar()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._iconBtn = UIFactory.gLoadedButton("", 0, 0, -1, -1, this);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.removeDrag();
            this._iconBtn.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            this._iconBtn.dispose(param1);
            this._iconBtn = null;
            super.disposeImpl(param1);
            return;
        }// end function

        public function setIconBtnStyle(param1:String, param2:Number, param3:Number) : void
        {
            this._iconBtn.styleName = param1;
            this._iconBtn.width = param2;
            this._iconBtn.height = param3;
            return;
        }// end function

        public function set isCanDrag(param1:Boolean) : void
        {
            this._isCanDrag = param1;
            if (this._isCanDrag && this._iconBtn)
            {
                this.addDragEvent();
            }
            else
            {
                this.removeDrag();
            }
            return;
        }// end function

        protected function addDragEvent() : void
        {
            this._iconBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            return;
        }// end function

        protected function onMouseDown(event:MouseEvent) : void
        {
            this._mouseStartX = event.stageX;
            this._iconBaseX = this._iconBtn.x;
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
            Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            return;
        }// end function

        protected function onMouseMove(event:MouseEvent) : void
        {
            var _loc_2:* = event.stageX;
            var _loc_3:* = _loc_2 - this._mouseStartX + this._iconBaseX;
            var _loc_4:* = _value / _totalValue;
            var _loc_5:* = this._baseX + _progressWidth;
            _loc_3 = _loc_3 < this._baseX ? (this._baseX) : (_loc_3);
            _loc_3 = _loc_3 > _loc_5 ? (_loc_5) : (_loc_3);
            var _loc_6:* = (_loc_3 - this._baseX) / (_loc_5 - this._baseX) * _totalValue;
            setValue(_loc_6, _totalValue);
            return;
        }// end function

        protected function onMouseUp(event:Event) : void
        {
            this.removeDrag();
            return;
        }// end function

        private function removeDrag() : void
        {
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
            Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            return;
        }// end function

        override public function setProgress(param1:String, param2:Boolean = true, param3:Number = 0, param4:Number = 0, param5:int = 0, param6:int = 0) : void
        {
            super.setProgress(param1, param2, param3, param4, param5, param6);
            this._baseX = param3;
            this._baseY = param4;
            this._baseHeight = param6;
            return;
        }// end function

        override public function drawBar() : void
        {
            super.drawBar();
            var _loc_1:* = _value / _totalValue;
            var _loc_2:* = _loc_1 * _progressWidth;
            var _loc_3:* = this._baseX + _loc_2 - this._iconBtn.width / 2;
            var _loc_4:* = this._baseY - this._iconBtn.height / 2 + this._baseHeight / 2;
            this._iconBtn.x = _loc_3;
            this._iconBtn.y = _loc_4;
            return;
        }// end function

    }
}
